package com.youi.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.GoodsizeDao;
import com.youi.entity.Goods;
import com.youi.entity.Goodssize;
import com.youi.entity.Ordergoods;
import com.youi.entity.Userorder;
import com.youi.model.GoodssizeObj;
import com.youi.service.GoodsService;
import com.youi.service.GoodssizeService;
import com.youi.service.OrdergoodsService;
import com.youi.service.UserorderService;
import com.youi.util.JedisUtils;

@Service
public class GoodssizeServiceImpl extends BaseServiceImpl<Goodssize> implements GoodssizeService{
		
		private GoodsizeDao goodsizeDao;
		@Autowired
		private GoodsService goodsService;
		@Autowired
		private OrdergoodsService ordergoodsService;
		@Autowired
		private UserorderService userorderService;
		@Autowired
		public void setfaultRecordDao(GoodsizeDao goodsizeDao){
			this.goodsizeDao=goodsizeDao;
			super.setBaseDAO(goodsizeDao);
		}

		@Override
		public Integer addGoodssize(Long goodsId,Goods goods,List<GoodssizeObj> goodssizeList) {
			// TODO Auto-generated method stub
			Integer allStockNum=0;
			if (goodssizeList!=null&&goodssizeList.size()>0) {
				Goodssize goodssize=null;
				for (GoodssizeObj goodssizeObj : goodssizeList) {
					if (goodssizeObj.getId()!=null) {
						goodssize=goodsizeDao.get(goodssizeObj.getId());
						if (goodssize!=null) {
							if (goodssizeObj.getSpecification()!=null&&goodssizeObj.getSpecification().trim().length()>0) {
								goodssize.setSpecification(goodssizeObj.getSpecification());
							}
							if (goodssizeObj.getPrice()!=null) {
								goodssize.setPrice(goodssizeObj.getPrice());
							}
							if (goodssizeObj.getStockNum()!=null) {
								if (goodssizeObj.getStockNum()<goodssize.getSaleNum()) {
									return 0;
								}
								goodssize.setStockNum(goodssizeObj.getStockNum());
							}
							if (goodssizeObj.getSaleNum()!=null) {
								goodssize.setSaleNum(goodssizeObj.getSaleNum());
							}
							if (goodssizeObj.getStockNum()!=null&&goodssizeObj.getSaleNum()!=null) {
								goodssize.setRealStockNum(goodssizeObj.getStockNum()-goodssizeObj.getSaleNum());
							}
							if (goodssizeObj.getDelStatus()!=null&&goodssizeObj.getDelStatus()==1) {
								goodssize.setDelStatus(DelStatus.DEL);
							}
							goodsizeDao.saveOrUpdate(goodssize);
						}
					}else {
						goodssize=new Goodssize(goods.getId(), goodssizeObj.getSpecification(), goodssizeObj.getPrice(), goodssizeObj.getStockNum());
						goodsizeDao.saveOrUpdate(goodssize);
					}
					allStockNum+=goodssize.getStockNum();
					if (goodssize.getDelStatus()==0) {
						JedisUtils.setObject(goodssize.getId().toString(), goodssize);
					}
				}
				goods.setRealStockCnt(allStockNum);
				goodsService.saveOrUpdate(goods);				
			}
			return 1;
		}
		
		/**
		 * 获取规格对应的订单销量
		 * @param goodsId
		 * @param goodssizeobej
		 * @return
		 */
		private Integer getOrdergoodsSaleNum(Long goodsId,GoodssizeObj goodssizeobej) {
			if (goodssizeobej!=null) {
				Map<String, Object> params=new HashMap<String, Object>();
				params.put(Common.DELSTATUS, DelStatus.NODEL);
				params.put("goodsId", goodsId);
				params.put("slaePrice", goodssizeobej.getPrice());
				params.put("specification", goodssizeobej.getSpecification());
				List<Ordergoods> ordergoodsList=ordergoodsService.getByParams(params);
				if (ordergoodsList!=null&&ordergoodsList.size()>0) {
					Integer count=0;
					for (Ordergoods ordergoods : ordergoodsList) {
						params=new HashMap<String, Object>();
						params.put(Common.DELSTATUS, DelStatus.NODEL);
						params.put(Common.ID, ordergoods.getOrderId());
						params.put(Common.STATUS, Arrays.asList(1,2,3,4,5));
						Userorder userorder=userorderService.getByParam(params);
						if (userorder!=null) {
							count+=ordergoods.getCount();					
						}
					}
					return count;
				}
			}
			return 0;
		}

		@Override
		public Goodssize getForUpdate(Long id) {
			// TODO Auto-generated method stub
			return goodsizeDao.getForUpdate(id);
		}
}